import java.util.*;
import java.util.stream.Collectors;

public class TreeBuilder {

    // 核心转换方法
    public static List<TreeNode> buildTree(List<SysMenu> menuList) {
        // 首先将菜单列表转换为节点映射，方便快速查找
        Map<Integer, TreeNode> nodeMap = menuList.stream()
                .map(SysMenu::toTreeNode)
                .collect(Collectors.toMap(TreeNode::getId, node -> node));

        // 遍历节点，构建父子关系
        List<TreeNode> rootNodes = new ArrayList<>();
        for (TreeNode node : nodeMap.values()) {
            Integer parentId = node.getParentId();
            if (parentId == null || !nodeMap.containsKey(parentId)) {
                // 没有父节点或父节点不存在，视为根节点
                rootNodes.add(node);
            } else {
                // 有父节点，将当前节点添加到父节点的子节点列表
                TreeNode parentNode = nodeMap.get(parentId);
                parentNode.getChildren().add(node);
            }
        }
        
        return rootNodes;
    }

    // 树节点类
    public static class TreeNode {
        private Integer id;
        private String name;
        private String type;
        private Integer parentId;
        private List<TreeNode> children = new ArrayList<>();

        // Getters and Setters
        public Integer getId() { return id; }
        public void setId(Integer id) { this.id = id; }
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
        public Integer getParentId() { return parentId; }
        public void setParentId(Integer parentId) { this.parentId = parentId; }
        public List<TreeNode> getChildren() { return children; }
        public void setChildren(List<TreeNode> children) { this.children = children; }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", type='" + type + '\'' +
                    ", parentId=" + parentId +
                    ", children=" + children.size() +
                    '}';
        }
    }

    // 系统菜单类（示例数据类）
    public static class SysMenu {
        private Integer id;
        private String name;
        private String type;
        private Integer parentId;

        public SysMenu(Integer id, String name, String type, Integer parentId) {
            this.id = id;
            this.name = name;
            this.type = type;
            this.parentId = parentId;
        }

        // 转换为树节点的方法
        public TreeNode toTreeNode() {
            TreeNode node = new TreeNode();
            node.setId(this.id);
            node.setName(this.name);
            node.setType(this.type);
            node.setParentId(this.parentId);
            return node;
        }

        // Getters for demo
        public Integer getId() { return id; }
        public String getName() { return name; }
        public String getType() { return type; }
        public Integer getParentId() { return parentId; }
    }

    // 测试示例
    public static void main(String[] args) {
        // 构建测试数据
        List<SysMenu> menuList = Arrays.asList(
            new SysMenu(2, "用户管理", "MENU", 1),
            new SysMenu(3, "角色管理", "MENU", 1),
            new SysMenu(4, "菜单管理", "MENU", 1),
            new SysMenu(5, "添加用户", "BUTTON", 2),
            new SysMenu(6, "编辑用户", "BUTTON", 2),
            new SysMenu(7, "数据统计", "MENU", null),
            new SysMenu(1, "系统管理", "MENU", null),
            new SysMenu(8, "用户统计", "MENU", 7)
        );

        // 构建树
        List<TreeNode> tree = buildTree(menuList);

        // 打印树结构（递归打印方法）
        printTree(tree, 0);
    }

    // 辅助方法：递归打印树结构
    private static void printTree(List<TreeNode> nodes, int level) {
        String indent = "  ".repeat(level);
        for (TreeNode node : nodes) {
            System.out.println(indent + node.getName());
            printTree(node.getChildren(), level + 1);
        }
    }
}